//
//  ChangePwdController.m
//  HarmonizePub
//
//  Created by rzhy on 2016/10/12.
//  Copyright © 2016年 Zy. All rights reserved.
//

#import "ChangePwdController.h"
#import "UIBarButtonItem+Zy.h"
#import "Config.h"

@interface ChangePwdController () <UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *oldPwdField;
@property (weak, nonatomic) IBOutlet UITextField *freshPwdField;
@property (weak, nonatomic) IBOutlet UITextField *confirmPwdField;

@end

@implementation ChangePwdController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"密码修改";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"修改" style:UIBarButtonItemStyleDone target:self action:@selector(changePwdEvent)];
    self.oldPwdField.returnKeyType = UIReturnKeyNext;
    self.freshPwdField.returnKeyType = UIReturnKeyNext;
    self.confirmPwdField.returnKeyType = UIReturnKeyDone;
    [self.oldPwdField becomeFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 修改
- (void)changePwdEvent {
    NSString *oldPwd = [self.oldPwdField.text stringRemoveSpecialSymbol:@" "];
    NSString *freshPwd = [self.freshPwdField.text stringRemoveSpecialSymbol:@" "];
    NSString *confirmPwd = [self.confirmPwdField.text stringRemoveSpecialSymbol:@" "];
    if (oldPwd.length < 1)
        [self setAlertViewWithMessage:@"请输入旧密码!"];
    else if (freshPwd.length < 1)
        [self setAlertViewWithMessage:@"请输入新密码!"];
    else if (confirmPwd.length < 1)
        [self setAlertViewWithMessage:@"请再输入一遍新密码!"];
    else if (![confirmPwd isEqualToString:freshPwd])
        [self setAlertViewWithMessage:@"两次输入的新密码不一致，请重新输入"];
    else {
        [self.view endEditing:NO];
        NSDictionary *cookie = @{CookieVaule : [UserDefaultsTool stringForKey:XHToken]};
        NSDictionary *param = @{@"oldPassword" : oldPwd, @"newPassword" : freshPwd};
        __unsafe_unretained ChangePwdController *weakSelf = self;
        [HTTPSTool POSTRequestWithPath:XHUpdatePwdPath cookie:cookie params:param success:^(NSDictionary * _Nonnull JSON) {
//            XHLog(@"密码修改 -> JSON: %@", JSON)
            [ZYToast showOperationSuccess];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }];
    }
}

#pragma mark - textField delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField == self.oldPwdField)
        [self.freshPwdField becomeFirstResponder];
    else if (textField == self.freshPwdField)
        [self.confirmPwdField becomeFirstResponder];
    else
        [self changePwdEvent];
    return YES;
}

#pragma mark AlertView
- (void)setAlertViewWithMessage:(NSString *)message {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提 示:" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:defaultAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

@end
